package com.demo.rabbitmq.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.lang.NonNull;

/**
 * 自动创建Exchange，队列，绑定Exchange和队列
 */
@Configuration
public class RabbitMQExchangeQueueConfig implements ApplicationContextAware {

    @Value("${spring.rabbitmq.extra.plugin.enable: false}")
    private Boolean enablePlugin;

    @Override
    public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException {
        CreateDirectExchangeQueue.init((GenericApplicationContext) applicationContext);
        CreateFanoutExchangeQueue.init((GenericApplicationContext) applicationContext);
        CreateDeadExchangeQueue.init((GenericApplicationContext) applicationContext);

        if (enablePlugin) {
            CreatePluginDirectExchangeQueue.init((GenericApplicationContext) applicationContext);
        }
    }
}
